﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWCInvoice.Servicelayer;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Drawing;
using SWCInvoice.ViewModel;
using Cyotek.GhostScript.PdfConversion;


namespace SWCInvoice.Model {
    public class Page : ModelBase, IDisposable {

        #region Fields

        /// <summary>
        /// ID of the page
        /// </summary>
        private string _id;
        /// <summary>
        /// Identifier of the page
        /// </summary>
        private string _identifier;
        /// <summary>
        /// Has the identifier a inheritance identifier
        /// </summary>
        private bool _hasIdentifierInheritance;
        /// <summary>
        /// Textual content of the page
        /// </summary>
        private string _content;
        /// <summary>
        /// Textual content of the page as a image
        /// </summary>
        private Bitmap _imageContent;
        /// <summary>
        /// The associated Document
        /// </summary>
        private Document _document;
        /// <summary>
        /// The original pagenumber of the page
        /// </summary>
        private int _pageNumberOriginal;
        /// <summary>
        /// The virtual pagenumber of the page
        /// </summary>
        private int _pageNumberVirtual;
        /// <summary>
        /// PDF content of the page
        /// </summary>
        private PdfImportedPage _pdfContent;

        private bool isDisposed = false;
        #endregion

        #region Constructor

        /// <summary>
        /// Constructor of the page object
        /// </summary>
        public Page(Document document) {
            _document = document;
            _id = Utilities.NewGuid();
        }

        #endregion

        #region Properties

        /// <summary>
        /// ID of the page
        /// </summary>
        /// <value>A GUID value</value>
        public string ID {
            get {
                return _id;
            }
        }

        /// <summary>
        /// Identifier of the page
        /// </summary>
        public Utils.Utils.IdentifierState HasIdentifier {
            get {
                if (!string.IsNullOrWhiteSpace(Identifier))
                    if (HasIdentifierInheritance)
                        return Utils.Utils.IdentifierState.IdentifierInheritance;
                    else
                        return Utils.Utils.IdentifierState.Identifier;
                else
                    return Utils.Utils.IdentifierState.None;                        
            }            
        }

        /// <summary>
        /// Has the identifier a inheritance identifier
        /// </summary>
        public bool HasIdentifierInheritance {
            get {
                return _hasIdentifierInheritance;
            }
            set {
                if (_hasIdentifierInheritance == value)
                    return;
                _hasIdentifierInheritance = value;
                RaisePropertyChanged("HasIdentifierInheritance");
                RaisePropertyChanged("HasIdentifier");
            }
        }

        /// <summary>
        /// Identifier of the page
        /// </summary>
        public string Identifier {
            get {
                return _identifier;
            }
            private set {
                if (_identifier == value)
                    return;
                _identifier = value;
                RaisePropertyChanged("Identifier");
                RaisePropertyChanged("HasIdentifier");
            }
        }

        /// <summary>
        /// Textual content of the page
        /// </summary>
        public string Content {
            get {
                return _content;
            }
            set {
                if (_content == value)
                    return;
                _content = value;
                RaisePropertyChanged("Content");
            }
        }

        /// <summary>
        /// Textual content of the page as a image
        /// </summary>
        public Bitmap ImageContent
        {
            get
            {
                if (_imageContent == null && _pageNumberOriginal > 0) {
                    _imageContent = _document.Pdf2Image.GetImage(_pageNumberOriginal);                                        
                    return _imageContent;
                } else {                    
                    return _imageContent;
                }
            }
            set
            {
                if (_imageContent == value)
                    return;
                _imageContent = value;
                RaisePropertyChanged("ImageContent");
            }
        }

        /// <summary>
        /// The associated Document
        /// </summary>
        public Document Document {
            get {
                return _document;
            }
        }

        /// <summary>
        /// The original pagenumber of the page
        /// </summary>
        public int PageNumberOriginal {
            get {
                return _pageNumberOriginal;
            }
            set {
                if (_pageNumberOriginal == value)
                    return;
                _pageNumberOriginal = value;
                RaisePropertyChanged("PageNumberOriginal");
            }
        }

        /// <summary>
        /// The virtual pagenumber of the page
        /// </summary>
        public int PageNumberVirtual {
            get {
                return _pageNumberVirtual;
            }
            set {
                if (_pageNumberVirtual == value)
                    return;
                _pageNumberVirtual = value;
                RaisePropertyChanged("PageNumberVirtual");
            }
        }

        /// <summary>
        /// PDF content of the page
        /// </summary>
        public PdfImportedPage PDFContent {
            get {
                return _pdfContent;
            }
            set {
                if (_pdfContent == value)
                    return;
                _pdfContent = value;
                RaisePropertyChanged("PDFContent");
            }
        }

        /// <summary>
        /// The identifier helpText;
        /// </summary>
        public string HelpText {
            get {
                //&#x0a;

                string identifier = string.Empty;
                string pageNumberOriginal = string.Empty;
                string pageNumberVirtual = string.Empty;

                if (!string.IsNullOrWhiteSpace(_identifier))
                    identifier = string.Format("{0}\t\t{1}\r\n", Properties.ResourceVirtualDocumentStructureView.TreeNodePageIdentifier, _identifier);
                if (_pageNumberOriginal != 0)
                    pageNumberOriginal = string.Format("{0}\t{1}\r\n", Properties.ResourceVirtualDocumentStructureView.TreeNodePagePageNumberOriginal, _pageNumberOriginal);
                if (_pageNumberVirtual != 0)
                    pageNumberVirtual = string.Format("{0}\t\t{1}\r\n", Properties.ResourceVirtualDocumentStructureView.TreeNodePagePageNumberVirtual, _pageNumberVirtual);

                return string.Format("{0}{1}{2}", identifier, pageNumberOriginal, pageNumberVirtual);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Set the identifier to the page
        /// </summary>
        /// <param name="identifierKey">identifierKey as a string</param>
        /// <param name="pattern">the regex search pattern</param>
        /// <returns>the identifier</returns>
        public string SetIdentifier(string identifierKey, Regex searchPattern) {
            MatchCollection matchCollection = null;
            string[] array = _content.Split(new char[] { '\n' });
            Identifier = String.Empty; 

            foreach (string item in array) {
                if (item.Contains(identifierKey)) {
                    if (searchPattern.IsMatch(item)) {
                        matchCollection = searchPattern.Matches(item);
                        Identifier = matchCollection[matchCollection.Count - 1].Value;
                        //_identifier = searchPattern.Match(item).Value;
                        return Identifier;
                    }
                }
            }
            return Identifier;
        }

        /// <summary>
        /// Set the identifier to the page
        /// </summary>        
        /// <param name="pattern">the regex search pattern</param>
        /// <returns>the identifier</returns>
        public string SetIdentifier(Regex searchPattern) {
            MatchCollection matchCollection = null;
            string[] array = _content.Split(new char[] { '\n' });
            Identifier = String.Empty;          

            foreach (string item in array) {
                if (searchPattern.IsMatch(item)) {
                    matchCollection = searchPattern.Matches(item);
                    Identifier = matchCollection[matchCollection.Count - 1].Value;
                    //_identifier = searchPattern.Match(item).Value;
                    return Identifier;
                }
            }
            return Identifier;
        }

        /// <summary>
        /// Set the identifier to the page
        /// </summary>
        /// <param name="identifier">the identifier</param>
        /// <returns>the identifier</returns>
        public string SetIdentifier(string identifier) {
            Identifier = identifier;

            return Identifier;
        }

        #endregion  
     
        #region Dispose

        protected void Dispose(bool disposing) {
            if (disposing) {
                // Code to dispose the managed resources of the class
                   
                //Identifier = null;                             
                //Content = null;
               
                ImageContent = null;
                PDFContent = null;
            }

            // Code to dispose the un-managed resources of the class
            isDisposed = true;
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
